/**
 @Name：大学生计算机课程平时成绩管理系统 班级管理
 @Author：dingwen

 */

layui.define(['table', 'form'], function (exports) {
    let $ = layui.$
        , table = layui.table
        , setter = layui.setter
        , form = layui.form;

    $(function () {
        let total = 0;
        /*获取总记录条数*/
        $.ajax({
            url: layui.setter.baseUrl + '/clazz/total',
            type: 'get',
            dateType: "json",
            contentType: 'application/json',
            success: function (res) {
                if (res.code === 200) {
                    total = res.data;
                    // 表格数据渲染
                    // 表格数据渲染
                    table.render({
                        elem: '#LAY-app-clazz'
                        , page: true
                        , url: layui.setter.baseUrl + '/clazz/info/page'
                        , parseData: function (res) {
                            return {
                                "code": 0,
                                "msg": res.message,
                                "count": res.data.length,
                                "data": res.data
                            };
                        }
                        , request: {
                            pageName: 'pageNum'
                            , limitName: 'pageSize'
                        }
                        , cols:
                            [[
                                {type: 'numbers', fixed: 'left'}
                                , {field: 'clazzId', width: 200, title: 'ID', sort: true}
                                , {field: 'clazzName', title: '名称', minWidth: 200}
                                , {field: 'clazzYear', title: '年级', minWidth: 100}
                                , {field: 'teacherId', title: '班主任教工号', minWidth: 200}
                                , {field: 'teacherName', title: '班主任', width: 100, style: 'background-color: #009688; color: #fff;'}
                                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#layuiadmin-app-clazz-bar'}
                            ]]
                    });
                } else {
                    layer.msg("失败");
                }
            }
        });
    });

    //监听
    table.on('tool(LAY-app-clazz)', function (obj) {
        let data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除 ' + data.clazzYear + data.clazzName + '？', function (index) {
                $.ajax({
                    url: setter.baseUrl + `/clazz/remove/${data.clazzId}`,
                    dataType: "json",
                    type: "delete",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("成功");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: '更新班级信息',
                content: 'clazz_modify.html',
                area: ['30%', '60%'],
                btn: ['提交', '取消'],
                btnAlign: 'c',
                yes: function (index) {
                    let dataArr = layer.getChildFrame("#modifyClazz").serializeArray();
                    //添加
                    $.ajax({
                        url: layui.setter.baseUrl + '/clazz/modify',
                        type: 'put',
                        data: serializeArrayToJson(dataArr),
                        dateType: "json",
                        contentType: 'application/json',
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg("成功");
                                layer.close(index);
                                //刷新
                                $(".layui-laypage-btn")[0].click();
                            } else {
                                layer.msg("失败");
                            }
                        }
                    });
                }, success: function (layero, index) {
                    const iframe = window['layui-layer-iframe' + index];
                    //调用子页面的全局函数
                    iframe.child(data);
                }
            });
        }
    });
    exports('clazz', {})
});
